package com.xkcyy.one.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xkcyy.one.entity.TrainingLog;
import com.xkcyy.one.exception.BusinessException;
import com.xkcyy.one.mapper.TrainingExampleMapper;
import com.xkcyy.one.mapper.TrainingLogMapper;
import com.xkcyy.one.mapper.TrainingSessionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 训练日志服务类
 */
@Service
public class TrainingLogService {
    
    private final TrainingLogMapper trainingLogMapper;
    private final TrainingSessionMapper trainingSessionMapper;
    private final TrainingExampleMapper trainingExampleMapper;
    
    @Autowired
    public TrainingLogService(TrainingLogMapper trainingLogMapper, TrainingSessionMapper trainingSessionMapper, TrainingExampleMapper trainingExampleMapper) {
        this.trainingLogMapper = trainingLogMapper;
        this.trainingSessionMapper = trainingSessionMapper;
        this.trainingExampleMapper = trainingExampleMapper;
    }
    
    /**
     * 根据会话ID获取日志列表
     *
     * @param sessionId 会话ID
     * @return 日志列表
     */
    public List<TrainingLog> getLogsBySessionId(Long sessionId) {
        // 检查会话是否存在
        if (trainingSessionMapper.selectById(sessionId) == null) {
            throw new BusinessException("训练会话不存在，ID: " + sessionId);
        }
        
        LambdaQueryWrapper<TrainingLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainingLog::getSessionId, sessionId);
        return trainingLogMapper.selectList(queryWrapper);
    }
    
    /**
     * 根据ID获取日志
     *
     * @param id 日志ID
     * @return 日志
     */
    public TrainingLog getLogById(Long id) {
        TrainingLog log = trainingLogMapper.selectById(id);
        if (log == null) {
            throw new BusinessException("训练日志不存在，ID: " + id);
        }
        return log;
    }
    
    /**
     * 创建日志 - 内部方法，只供TrainingService调用
     *
     * @param log 日志
     * @return 创建后的日志
     */
    protected TrainingLog createLog(TrainingLog log) {
        // 检查会话是否存在
        if (trainingSessionMapper.selectById(log.getSessionId()) == null) {
            throw new BusinessException("训练会话不存在，ID: " + log.getSessionId());
        }
        
        // 检查样例是否存在
        if (trainingExampleMapper.selectById(log.getExampleId()) == null) {
            throw new BusinessException("训练样例不存在，ID: " + log.getExampleId());
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        log.setCreateTime(now);
        log.setUpdateTime(now);
        
        // 插入日志
        trainingLogMapper.insert(log);
        
        return log;
    }
    
    /**
     * 批量创建日志 - 内部方法，只供TrainingService调用
     *
     * @param logs 日志列表
     * @return 创建后的日志列表
     */
    protected List<TrainingLog> createLogBatch(List<TrainingLog> logs) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        for (TrainingLog log : logs) {
            // 检查会话是否存在
            if (trainingSessionMapper.selectById(log.getSessionId()) == null) {
                throw new BusinessException("训练会话不存在，ID: " + log.getSessionId());
            }
            
            // 检查样例是否存在
            if (trainingExampleMapper.selectById(log.getExampleId()) == null) {
                throw new BusinessException("训练样例不存在，ID: " + log.getExampleId());
            }
            
            log.setCreateTime(now);
            log.setUpdateTime(now);
            
            // 插入日志
            trainingLogMapper.insert(log);
        }
        
        return logs;
    }
    
    /**
     * 更新日志
     *
     * @param log 日志
     * @return 更新后的日志
     */
    public TrainingLog updateLog(TrainingLog log) {
        // 检查日志是否存在
        TrainingLog existingLog = trainingLogMapper.selectById(log.getId());
        if (existingLog == null) {
            throw new BusinessException("训练日志不存在，ID: " + log.getId());
        }
        
        // 不允许修改会话ID和样例ID
        log.setSessionId(existingLog.getSessionId());
        log.setExampleId(existingLog.getExampleId());
        
        // 设置更新时间
        log.setUpdateTime(LocalDateTime.now());
        
        // 更新日志
        trainingLogMapper.updateById(log);
        
        return log;
    }
    
    /**
     * 删除日志
     *
     * @param id 日志ID
     */
    public void deleteLog(Long id) {
        // 检查日志是否存在
        TrainingLog existingLog = trainingLogMapper.selectById(id);
        if (existingLog == null) {
            throw new BusinessException("训练日志不存在，ID: " + id);
        }
        
        // 删除日志
        trainingLogMapper.deleteById(id);
    }
    
    /**
     * 删除会话的所有日志 - 内部方法，只供TrainingService调用
     *
     * @param sessionId 会话ID
     */
    protected void deleteLogsBySessionId(Long sessionId) {
        // 检查会话是否存在
        if (trainingSessionMapper.selectById(sessionId) == null) {
            throw new BusinessException("训练会话不存在，ID: " + sessionId);
        }
        
        LambdaQueryWrapper<TrainingLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainingLog::getSessionId, sessionId);
        trainingLogMapper.delete(queryWrapper);
    }
} 